home *** CD-ROM | disk | FTP | other *** search
- Path: mips.complang.tuwien.ac.at!schwarz
- From: schwarz@mips.complang.tuwien.ac.at (Konrad Schwarz)
- Newsgroups: comp.std.c
- Subject: integral types in switch expressions
- Date: 26 Jan 1996 18:17:05 GMT
- Organization: TU Wien
- Sender: schwarz@a3.complang.tuwien.ac.at (Konrad Schwarz)
- Message-ID: <4eb5r1$b04@news.tuwien.ac.at>
- NNTP-Posting-Host: a3.complang.tuwien.ac.at
-
- I was surprised to learn that constant pointer expressions are
- not allowed in case labels and that the expression in a switch statement
- must be an integral type. (The reason for the exclusion of floating point
- is obvious to me). I am interested in the technical reasons for this.
-
- Consider:
-
- T a [] = { ... }, *b = a + sizeof a / sizeof *a, *c;
-
- switch (c) {
- case a: ...
- case a + 1: ...
- case a + 3: ...
- default: ...
- }
-
- where T is some type.
- The assignment to b has worked on all compilers I have ever tried it on
- (even and especially K&R I). I take this as a strong indication of
- conformance. The switch construct above is explicitly disallowed, although
- there does not seem to be much difference in the expressions themselves.
-
- I am of course aware that
-
- switch (c - a) {
- case 0: ...
- case 1: ...
- case 3: ...
- default: ...
- }
-
- is equivalent code, but it suffers from the division needed to evaluate
- c - a.
-
- Konrad Schwarz
-